//
//  STDoorInfoView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/9.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STDoorInfoView.h"
#import "STDoorStarCell.h"
#import "STNameHeaderCell.h"
#import "STDoorAdrCell.h"
#import "STDoorEvaCell.h"
#import "UIButton+ZYHblock.h"

@implementation STDoorInfoView{
}

-(void)setShopInViewWithData:(id)data{
    shopInfoDic = data;
    if(self.isSelfDoor){
       rowHeight = @[@[@200,@80,@80,@30],@[@50,@100,@30],@[@50,@50,@30],@[@50,@150]];
    }else{
       rowHeight = @[@[@200,@80,@80],@[@50,@100,@30],@[@50,@50],@[@50,@150]];
    }
        
    
    sectionHeaderNames= @[@"入驻美发师",@"门店服务范围",@"顾客评价"];
    [self loadHouseView];
}

-(void)loadHouseView{
    if(!shopTableView){
        shopTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        shopTableView.delegate = self;
        shopTableView.dataSource = self;
        shopTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        shopTableView.backgroundColor = [UIColor clearColor];
        shopTableView.tableFooterView = [UIView new];
        shopTableView.sectionHeaderHeight = 10;
        [self addSubview:shopTableView];
    }else{
        [shopTableView reloadData];
    }
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return rowHeight.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * arrSection = rowHeight[section];
    return arrSection.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == (rowHeight.count -1)) {
        return 0;
    }
    return 10;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 2 && indexPath.row ==1){
        NSInteger count = shopInfoDic.serviceInfo.catergoryNames.count;
        if (count) {
            return ceil(count/4.0) * 50;
        }
    }
        
    NSArray * arrSection = rowHeight[indexPath.section];
    NSNumber * height = arrSection[indexPath.row];
    return [height floatValue];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString * advIdentifier = @"advIdentifier";
            cell = [tableView dequeueReusableCellWithIdentifier:advIdentifier];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:advIdentifier];
            }
            [self advScrollViewWithData:shopInfoDic.localInfo.storePhotos];
            if(![cell.contentView.subviews containsObject:advScrollView]){
                [cell.contentView addSubview:advScrollView];
            }
        }else if (indexPath.row == 1) {
            static NSString * starIdentifier = @"starIdentifier";
            STDoorStarCell * cell =[tableView dequeueReusableCellWithIdentifier:starIdentifier];
            if (!cell) {
                cell = [[STDoorStarCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:starIdentifier];
            }
            [cell setStarViewWithData:shopInfoDic.localInfo];
            return cell;
        }else if (indexPath.row == 2) {
            
            static NSString * adrIdentifier = @"adrIdentifier";
            STDoorAdrCell * cell =[tableView dequeueReusableCellWithIdentifier:adrIdentifier];
            if (!cell) {
                cell = [[STDoorAdrCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:adrIdentifier];
            }
            [cell setDoorAdrInfoWith:shopInfoDic.localInfo];//
            return cell;
        }else if (indexPath.row == 3) {
            static NSString *identifier=@"STMoreInfoCell";
            cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell==nil) {
                cell= [[[NSBundle mainBundle]loadNibNamed:@"STMoreInfoCell" owner:nil options:nil] firstObject];
            }
            UIButton * btnMore = [cell viewWithTag:1013];
            [btnMore setTitle:@"前往编辑" forState:UIControlStateNormal];
        }
    }else if (indexPath.row == 0) {
        static NSString * headIdentifier = @"STNameHeaderCell";
        [tableView registerNib:[UINib nibWithNibName:@"STNameHeaderCell" bundle:nil] forCellReuseIdentifier:@"STNameHeaderCell"];
        STNameHeaderCell * cell =[tableView dequeueReusableCellWithIdentifier:headIdentifier];
        [cell.nameBtn setTitle:sectionHeaderNames[indexPath.section-1] forState:UIControlStateNormal];
        return cell;
    }else if (indexPath.section == 1) {
         if (indexPath.row == 1) {
             static NSString * advIdentifier = @"advIdentifier";
             cell = [tableView dequeueReusableCellWithIdentifier:advIdentifier];
             if (!cell) {
                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:advIdentifier];
             }
             [self setDoorHairWithData:shopInfoDic.hairerInfos indexPath:indexPath];
             if (![cell.contentView.subviews containsObject:csScrollView]) {
                 [cell.contentView addSubview:csScrollView];
             }
        }else if (indexPath.row == 2) {
            static NSString *identifier=@"STMoreInfoCell";
            cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell==nil) {
                cell= [[[NSBundle mainBundle]loadNibNamed:@"STMoreInfoCell" owner:nil options:nil] firstObject];
            }
            UIButton * btnMore = [cell viewWithTag:1013];
            [btnMore setTitle:@"查看更多" forState:UIControlStateNormal];
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            static NSString * advIdentifier = @"advIdentifier";
            cell = [tableView dequeueReusableCellWithIdentifier:advIdentifier];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:advIdentifier];
            }
            [self setDoorSerWithData:shopInfoDic.serviceInfo.catergoryNames indexPath:indexPath];
            if (![cell.contentView.subviews containsObject:showColView]) {
                [cell.contentView addSubview:showColView];
            }
        }else if (indexPath.row == 2) {
            static NSString *identifier=@"STMoreInfoCell";
            cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell==nil) {
                cell= [[[NSBundle mainBundle]loadNibNamed:@"STMoreInfoCell" owner:nil options:nil] firstObject];
            }
            UIButton * btnMore = [cell viewWithTag:1013];
            [btnMore setTitle:@"前往编辑" forState:UIControlStateNormal];
        }
    }else if (indexPath.section == 3) {
        if (indexPath.row == 1) {
            static NSString * evaIdentifier = @"evaIdentifier";
            STDoorEvaCell * cell =[tableView dequeueReusableCellWithIdentifier:evaIdentifier];
            if (!cell) {
                cell = [[STDoorEvaCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:evaIdentifier];
            }
            [cell setEvaluateView:shopInfoDic.commonInfo];
            WeakSelf
            cell.commentBtn.didSelectBtn = ^(UIButton *sender) {
                [weakSelf actionTurnToAllCommentVC];
            };
            return cell;
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

-(SDCycleScrollView *)advScrollViewWithData:(NSArray *)data{
    if (!advScrollView) {
        advScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) delegate:self placeholderImage:[UIImage imageNamed:@"m-adv"]];
        advScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
        advScrollView.delegate = self;
        advScrollView.autoScroll = NO;
        advScrollView.imageURLStringsGroup = data;
//        advScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }else{
        advScrollView.imageURLStringsGroup = data;
        [advScrollView reloadScrollView];
    }
    return advScrollView;
}

-(SHChangeSizeScrollView *)setDoorHairWithData:(id)data indexPath:(NSIndexPath *)indexPath{
    if (!csScrollView) {
        NSArray * arr =rowHeight[indexPath.section];
        NSNumber * height =arr[indexPath.row];
        csScrollView=[[SHChangeSizeScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [height floatValue])];
        csScrollView.delegate = self;
        csScrollView.isChangeSize = NO;
        csScrollView.intervalWight = 10;
        csScrollView.viewClassName = @"STHairerHeadView";
        csScrollView.showBounds = CGRectMake(0, 0, 80, 100);
        [csScrollView setScrollViewWithArrData:data];
    }else{
        [csScrollView setScrollViewWithArrData:data];
    }
    return csScrollView;
}


-(STShowColListView*)setDoorSerWithData:(id)data indexPath:(NSIndexPath *)indexPath{
    if(!showColView){
        NSArray * arr =rowHeight[indexPath.section];
        NSNumber * height =arr[indexPath.row];
        showColView=[[STShowColListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [height floatValue])];
        //    showColView.delegate = self;
        showColView.lineNum = 4;
        float w = (SCREEN_WIDTH-(5*10+1)) / 4;
        float h = 30;
        showColView.cellSize = CGSizeMake(w, h);
        showColView.showViewClassName = @"STDoorSerNameCell";
        showColView.alimentType = ZYHShowCollectionTypeAlimentCenter;
        [showColView setShowCollectionViewWithData:data];
    }else{
        [showColView setShowCollectionViewWithData:data];
    }
    return showColView;
}

//查看更多评价
-(void)actionTurnToAllCommentVC{
    if ([self.delegate respondsToSelector:@selector(doorInfoViewSeeMoreComment)]) {
        [self.delegate doorInfoViewSeeMoreComment];
    }
}


-(void)csScrollView:(SHChangeSizeScrollView*)scrollView detailView:(int)detailNum{
    if ([self.delegate respondsToSelector:@selector(doorInfoViewSelHairDic:)]) {
        [self.delegate doorInfoViewSelHairDic:detailNum];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0 && indexPath.row == 3) {
        if ([self.delegate respondsToSelector:@selector(doorInfoViewSetDoorInfo)]) {
            [self.delegate doorInfoViewSetDoorInfo];
        }
    }else if (indexPath.section ==1 && indexPath.row == 2) {
        if ([self.delegate respondsToSelector:@selector(doorInfoViewSeeMoreHair)]) {
            [self.delegate doorInfoViewSeeMoreHair];
        }
    }else if (indexPath.section ==2 && indexPath.row == 2) {
        if ([self.delegate respondsToSelector:@selector(doorInfoViewSetDoorService)]) {
            [self.delegate doorInfoViewSetDoorService];
        }
    }
}
@end
